<template>
	<view class="fxcp-box">
		<view class="tishi">
			以下一系列问题可在您选择合适的私募基金前，协助评估您的风险承受能力，
			理财方式和投资目标
		</view>
		<view class="ques-box">
			<view class="ques-item" v-for="(item,key) in questions" :key="key">
				<view class="ques-title">
					{{item.title}}
				</view>
				<view class="ques-answer-box">
					<view class="ques-answer-item" v-for="(value,index) in item.answers" @click="select(key,index,value.subject_No,value.option_Id,value.score)" :class="{'focus' :questions[key].select==index}">
						<view class="ques-answer">
							{{value.answer}}.............({{value.score}}分)
						</view>
						
					</view>
				</view> 
			</view>
		</view>
		<view class="showImg">
			<addImage @click="showImg"></addImage>
		</view>
		<view class="questionBox">
			<view class="question" @click="toCepingjieguo">
				<label class="iconfont">&#xe66a;</label>提交
			</view>
		</view>
	</view>
</template>

<script>
	import global from '../../../../../../components/tool/homePageQuery.vue';
	import global_ from '../../../../../../components/tool/licaishiwyx.vue';
	import addImage from '../../../../../API/image/image.vue';
	export default {
		components:{
			addImage
		},
		data() {
			return {
				selectAllList:[],
				array: [],
				questions:[
					/* {
					select:false,
					id:1,
					title:'1.您的职业稳定性如何',
					answers:[{
						xuanxiang:'A',
						answer:'很稳定',
						subject_No:'',
						option_Id:'',
						score:''
					},
					{
						xuanxiang:'A',
						answer:'稳定',
						subject_No:'',
						option_Id:'',
						score:''
					},{
						xuanxiang:'A',
						answer:'一般稳定',
						subject_No:'',
						option_Id:'',
						score:''
					}
					]
				} */],
				token:uni.getStorageSync('token_key'),
				news_Id:'',//客户号
				score:'',//风险测评得分
				imageList:[],//选择的图片路径
			};
		},
		onLoad(e){
			this.news_Id = e.customerNo;
			console.log("客户详情--客户号----"+e.customerNo+'-----'+this.news_Id);
			console.log("测试------"+JSON.stringify(this.array)+"------"+JSON.stringify(this.selectAllList))
			this.getRiskQuestionList();
		},
		methods:{
			showImg(e){
				console.info("所选择的图片：》》》》》》》"+e.imageList);
				this.imageList = e.imageList;
			},
			toCepingjieguo(){
				if(this.imageList.length==0){
					console.log("未上传附件-------"+this.imageList.length);
					uni.showToast({
						title: '请上传附件',
						icon: 'none',
						duration:2000,
						success: res => {},
						fail: () => {},
						complete: () => {}
					});
					return;
				}else{
					uni.request({
						method: 'POST',
						url: global.api.addRiskConsole,
						header : {'content-type':'application/x-www-form-urlencoded'},
						data: {
							token: this.token,
							customerNo: this.news_Id,
							selectListJson:JSON.stringify(this.array)
						},
					 
						dataType: 'json',
						success: (res) => {
							console.log("提交风险测评结果---"+JSON.stringify(res.data.code)+"-----"+JSON.stringify(res.data.message));
							console.log("风险测评得分-----"+JSON.stringify(res.data.data.totalScore));
							console.log("风险测评ID-----"+JSON.stringify(res.data.data.ID));

							this.score=res.data.data.totalScore;
							var result= res.data.data.totalScore;
							var answerID = res.data.data.ID;
							if(res.data.code =='0'){
								var imageList = this.imageList;
								console.info(imageList.length)
								// var jsonFile = JSON.stringify(that.tempFileList);
								var uploadStatus = true;
								if(imageList.length!=0){
									for(var i=0;i<imageList.length;i++){
										console.info("正在上传第"+(i+1)+"张图片")
										uni.uploadFile({
											url:global_.api.uploadPlImg,
											filePath:this.imageList[i],
											name: 'file',
											header: {
												'content-type': 'multipart/form-data'
											},
											formData:{
												proj_id:this.news_Id,//客户号
												answerId:answerID,//调查问卷id
												yewuType:'lcsFXCE',//标识
												orderNo:i+1,//序号
												token:this.token
											},  
											success: (res) => { 
												console.info('上传成功')
											},  
											fail: (err) =>{
												console.info('上传失败')
												uploadStatus = false;
											} 
										})
									}
								}
								if(uploadStatus){
									 uni.showToast({
										title: '成功',
										icon: 'success',
										success: res => {},
										fail: () => {},
										complete: () => {}
									});
									function sleep2(ms) {
										return new Promise(function(resolve, reject) {
											setTimeout(resolve, ms)
										})
									}
									sleep2(900).then(() => {
										uni.navigateTo({
												url: '../fenxianceping/cepingjieguo?customerNo='+this.news_Id+"&cpscore="+result,
												success: res => {},
												fail: () => {},
												complete: () => {}
										});
									})
								}
							}else{
								console.log("提交风险测评结果")
									uni.showToast({
										title: res.data.message,
										duration:2000,
										icon: 'none',
										success: res => {},
										fail: () => {},
										complete: () => {}
									});
							}
						},
						fail: (res) => {
							console.log("..............................................fail");
							console.log("提交风险测评结果---"+JSON.stringify(res.data.code)+"-----"+JSON.stringify(res.message));
							console.log(JSON.stringify(res));
							uni.showLoading({
								title: res.message
							});
							setTimeout(function () {
								uni.hideLoading();
							}, 1000);
							//this.text = 'request fail';
						},
						complete: (res) => {
							//停止当前页面下拉刷新。
							//uni.hideNavigationBarLoading();
							uni.stopPullDownRefresh();
						}
					}) 
				}
				 
				
			},
			getRiskQuestionList(){
				uni.request({
						method: 'POST',
						url: global.api.queryRiskQuestion,
						header : {'content-type':'application/x-www-form-urlencoded'},
						data: {
							token: this.token
						},
						dataType: 'json', 
						success: (res) => {
							console.log("风险测评问卷List---"+JSON.stringify(res.data.code)+"-----");
							if(res.data.code =='0'){
								var quTitleNoList = res.data.data.riskSubjectList; //题目编号
								//console.log("风险测评查询成功----------"+quertionList.length+"==="+JSON.stringify(quertionList));
								var siteMap = eval(res.data.data.riskSubjectList);
								//console.log("查询成功进入"+JSON.stringify(siteMap));
								var cpTitlefinal = new Array();
								var oncexxList = new Array();//初始默认选择第一项
								for(var j=0;j<siteMap.length;j++){
								　　var newsiteMap = siteMap[j];
									//console.log("遍历1------"+JSON.stringify(newsiteMap.key));
								　　for ( var key in newsiteMap ){ 
										//console.log("遍历2------"+JSON.stringify(newsiteMap[key])+"Key----"+key);
										var itemList = newsiteMap[key];
										var cpTitle = new Object();
										cpTitle.select= false;
										cpTitle.id = j;
										cpTitle.title = key;
										var oneXXList = new Array();
										for(var i=0;i<itemList.length;i++) {
											var xuanxianCon = new Object
											xuanxianCon.xuanxiang = '';//选项编号
											xuanxianCon.answer = itemList[i].question_Content;//选项内容
											xuanxianCon.subject_No = itemList[i].subject_No;//题目题号
											xuanxianCon.option_Id = itemList[i].option_Id;//答案ID
											xuanxianCon.score = itemList[i].score;//得分
											//console.log("题目选项内容---"+itemList[i].question_Content);
											oneXXList.push(xuanxianCon);
											if(i==0){//第一项默认选中状态
												var selectFirst = new Object;
												selectFirst.subject_No=itemList[i].subject_No;
												selectFirst.option_Id=itemList[i].option_Id;
												selectFirst.score=itemList[i].score;
												oncexxList.push(selectFirst);
											}
										}
										cpTitle.answers = oneXXList;
										cpTitlefinal.push(cpTitle);
									}
									
								}
								this.questions = cpTitlefinal;
								this.array = oncexxList;
								console.log("默认选中第一项-------"+JSON.stringify(oncexxList));
							} 
						}, 
						fail: (res) => {
							console.log("..............................................fail");
							console.log("风险测评问卷List---"+JSON.stringify(res.data.code)+"-----"+JSON.stringify(res.message));
							console.log(JSON.stringify(res));
							uni.showLoading({
								title: res.message
							});
							setTimeout(function () {
								uni.hideLoading();
							}, 1000);
							//this.text = 'request fail';
						},
						complete: (res) => {
							//停止当前页面下拉刷新。
							//uni.hideNavigationBarLoading();
							uni.stopPullDownRefresh();
						}
					})
			},
			select(key,index,subject_No,option_Id,score){
				this.questions[key].select=index;
				var finalXXList = this.array;//原始总集合
				var oncexxList = new Array();
				var selectXX = new Object;
				selectXX.subject_No=subject_No;
				selectXX.option_Id=option_Id;
				selectXX.score=score;
				oncexxList.push(selectXX);
				
				console.log("全局集合长度-----"+finalXXList.length);
				console.log("每次选项的list----1-----"+JSON.stringify(oncexxList))
				var flag = false;
				var firstAdd = false;
				if(finalXXList.length!=0){ 
					for(var i=0;i<oncexxList.length;i++) {
						for(var j=0;j<finalXXList.length;j++) {
							if(oncexxList[i].subject_No == finalXXList[j].subject_No){
								console.log("循环去重进行时----------")
								finalXXList.splice(j, 1);
								console.log("去重后-------"+JSON.stringify(finalXXList));
								flag =true;
							}
						}
					}
				}else{//第一次选择
					console.log("第一次选择-------------")
					var selectXX = new Object;
					selectXX.subject_No=subject_No;
					selectXX.option_Id=option_Id;
					selectXX.score=score;
					finalXXList.push(selectXX);
					firstAdd = true;
				} 
				if(!firstAdd){
					if(flag){
						console.log("覆盖旧选项-------");
						var selectXX = new Object;
						selectXX.subject_No=subject_No;
						selectXX.option_Id=option_Id;
						selectXX.score=score;
						finalXXList.push(selectXX);
					}else{
						console.log("新增新选项-------");
						var selectXX = new Object;
						selectXX.subject_No=subject_No;
						selectXX.option_Id=option_Id;
						selectXX.score=score;
						finalXXList.push(selectXX);
					}
				} 
				this.array = finalXXList;
				console.log("集合list---------"+JSON.stringify(this.array));
			
			}
		}
	}
</script>

<style>
	.focus{
		background: #eee;
	}
	.tishi {
		padding: 30upx;
		font-size: 12px;

		font-weight: 500;
		color: rgba(153, 153, 153, 1);
		line-height: 34upx;
	}

	.ques-box {
		background: rgb(255, 255, 255);
		
	}

	.ques-title {
		font-size: 15px;
		font-weight: bold;
		color: rgba(51, 51, 51, 1);
		line-height: 80upx;
		padding:0 42upx
	}

	.ques-answer-box {
		
		
	}

	.ques-answer-item {

		/* text-indent: 60upx; */
		font-weight: 500;
		color: rgba(102, 102, 102, 1);
		line-height: 50upx;
	}
	.ques-answer {
		width: 660upx;
		margin-left: 50upx;
	}
	.questionBox{
		display: flex;
		flex-direction: row;
		justify-content: center;
		width:100%;
		padding: 40upx 0;
		text-align: center;
		background: rgb(255, 255, 255);
		
	}
	.question {
		width: 240upx;
		height: 70upx;
		background: linear-gradient(-90deg, rgba(196, 156, 90, 1) 0%, rgba(226, 196, 138, 1) 100%);
		border-radius: 35px;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		color: rgb(255,255,255);
		
	}
	.showImg {
		padding-left: 50upx;
		background: #ffffff;
	}
	label{
		margin-right: 10upx;
	}
</style>
